using System;
using System.Collections.Generic;
using Com.queryzer2.Domain;
using Com.queryzer2.Domain.Entities;
using Com.queryzer2.Dao.Interface;
using Com.queryzer2.Dao.Base;
using Oracle.DataAccess.Client;
using System.Data;

namespace Com.queryzer2.Dao.Impl
{
    public class ServizioDao : OracleDao<OracleSessionManager, ISessionModel>, IServizioDao
    {

        private String selectByPOD;
        private string selectExistingPratica;
        private string selectByDISP_POD;

        public ServizioDao(OracleSessionManager daoContext)
            : base(daoContext)
        {
            if (!base.Context.Session_isActive())
            {
                base.Context.Session_Init();
                base.CurrentConnection.Open();
            }
        }
                
        public void Insert(ServizioEntity entity)
        {
            
        }

        public void Update(ServizioEntity entity)
        {
            
        }

        public void Delete(decimal id)
        {

        }

        public Dictionary<decimal, ServizioEntity> GetAll()
        {
            return null;
        }

        public ServizioEntity GetById(decimal id)
        {
            return null;
        }
        
        public ServizioEntity getExistingPratica(string cQueryFileName,string Cod_Prat_Utente,string Piva_Distributore)
        {
            ServizioEntity oRtn;
            selectExistingPratica = LoadQuery(cQueryFileName);
            Oracle.DataAccess.Client.OracleCommand cmd = new Oracle.DataAccess.Client.OracleCommand(selectExistingPratica, CurrentConnection);
            cmd.Parameters.Add("pCod_Prat_Utente", Cod_Prat_Utente);
            cmd.Parameters.Add("pPiva_Distributore", Piva_Distributore);

            oRtn = null;
            try
            {
                using (OracleDataReader r = cmd.ExecuteReader())
                {
                    if (r.HasRows)
                    {
                        r.Read();
                        oRtn = DaoOracleDbHelper.MapServizi(2,r);
                    }
                }
            }
            catch (Exception e0x)
            {
                throw new ManagedException("EX-UT001", e0x);
            }
            return oRtn;
        }

        public ServizioEntity findByPOD(string cQueryFileName, string cPod)
        {
            ServizioEntity oRtn;
            selectByPOD = LoadQuery(cQueryFileName);
            Oracle.DataAccess.Client.OracleCommand cmd = new Oracle.DataAccess.Client.OracleCommand(selectByPOD, CurrentConnection);
            //cmd.Parameters.Add("pPOD", cPod);
            cmd.Parameters.Add("pCodiceAllaccia",cPod.Substring(6,7));
            cmd.Parameters.Add("pProgrUtenza", cPod.Substring(13, 1));

            oRtn = null;
            try
            {
                using (OracleDataReader r = cmd.ExecuteReader())
                {
                    if (r.HasRows)
                    {
                        r.Read();
                        oRtn = DaoOracleDbHelper.MapServizi(1,r);
                    }
                }
            }
            catch (Exception e0x)
            {
                throw new ManagedException("EX-UT001", e0x);
            }
            return oRtn;
        }

        public ServizioEntity findBygetDataByDISP_POD(string cQueryFileName, string cPOD, string cContrDisp)
        {
            ServizioEntity oRtn;
            selectByDISP_POD = LoadQuery(cQueryFileName);
            Oracle.DataAccess.Client.OracleCommand cmd = new Oracle.DataAccess.Client.OracleCommand(selectByDISP_POD, CurrentConnection);
            //cmd.Parameters.Add("pPOD", cPod);
            cmd.Parameters.Add("pCodiceAllaccia", cPOD.Substring(6, 7));
            cmd.Parameters.Add("pProgrUtenza", cPOD.Substring(13, 1));
            cmd.Parameters.Add("pContrDisp", cContrDisp);

            oRtn = null;
            try
            {
                using (OracleDataReader r = cmd.ExecuteReader())
                {
                    if (r.HasRows)
                    {
                        r.Read();
                        oRtn = DaoOracleDbHelper.MapServizi(3, r);
                    }
                }
            }
            catch (Exception e0x)
            {
                throw new ManagedException("EX-UT001", e0x);
            }
            return oRtn;
        }

        public void Dispose()
        {
            if (!base.Context.TransactionModeOn)
            {
                base.CurrentConnection.Close();
            }
        }

        private OracleParameter[] MappingParams(UtenteEntity r, bool isInsert)
        {
            OracleParameter[] oparams;
            if (isInsert)
                oparams = new OracleParameter[9];
            else oparams = new OracleParameter[8];
            
            oparams[0] = new OracleParameter("pUSERNAME", OracleDbType.NVarchar2, r.Username, ParameterDirection.Input);
            oparams[1] = new OracleParameter("pPASSWORD", OracleDbType.NVarchar2, r.Password, ParameterDirection.Input);
            oparams[2] = new OracleParameter("pNOME", OracleDbType.NVarchar2, r.Nome, ParameterDirection.Input);
            oparams[3] = new OracleParameter("pCOGNOME", OracleDbType.NVarchar2, r.Cognome, ParameterDirection.Input);
            oparams[4] = new OracleParameter("pISADMIN", OracleDbType.Int32, Convert.ToInt32(r.IsAdmin), ParameterDirection.Input);
            oparams[5] = new OracleParameter("pROLEC", OracleDbType.Int32, Convert.ToInt32(r.RoleC), ParameterDirection.Input);
            oparams[6] = new OracleParameter("pROLEV", OracleDbType.Int32, Convert.ToInt64(r.RoleV), ParameterDirection.Input);
            if (isInsert)
            {
                oparams[7] = new OracleParameter("pID", OracleDbType.Decimal, r.Id, ParameterDirection.Output);
            }
            else
                oparams[7] = new OracleParameter("pID", OracleDbType.Decimal, r.Id, ParameterDirection.Input);

            return oparams;
        }

        public string LoadQuery(string cFileName)
        {
            System.IO.StreamReader sr;
            string cQuery;

            cQuery = "";
            sr = new System.IO.StreamReader(cFileName);
            while (!sr.EndOfStream)
            {
                cQuery = string.Concat(cQuery, " ", sr.ReadLine());
            }
            sr.Close();
            return cQuery;
        }

    }
}
